package org.hxyjs.service.impl;

import org.hxyjs.dao.FestivalDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Festival;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.FestivalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("FestivalService")
public class FestivalServiceImpl implements FestivalService {
    @Resource
    FestivalDao festivalDao;
    @Override
    public DTO<Festival> getAllFestivals()  throws Exception{
        List<Festival> festivals = festivalDao.getAllFestivals();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(festivals).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Festival>(messageEnum.getCode(),messageEnum.getMessage(),festivals);
    }

    @Override
    public DTO<Festival> deleteFestivalByFno(String fno)  throws Exception{
        Integer result = festivalDao.deleteFestivalByFno(fno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Festival>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Festival> updateFestivalByFno(Festival festival) throws Exception {
        Integer result = festivalDao.updateFestivalByFno(festival);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Festival>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Festival> addFestivalByFno(Festival festival) throws Exception {
        Integer result = festivalDao.addFestivalByFno(festival);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Festival>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param 
     * @return
     * @throws Exception
     */


    @Override
    public DTO<Festival> getFestivalByFno(String fno)  throws Exception{
        Festival festivals = festivalDao.getFestivalByFno(fno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(festivals).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Festival>(messageEnum.getCode(),messageEnum.getMessage(),festivals);
    }

    @Override
    public DTO<Festival> getFestivalByPage(PageInfo pageInfo) throws Exception {
        List<Festival> festivals = festivalDao.getFestivalByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(festivals).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Festival>(messageEnum.getCode(),messageEnum.getMessage(),festivals,pageInfo);
    }
}
